import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class SelectSort {
    public static void main(String[] args) {
        List<User> list = new ArrayList<>();
        User u1 = new User(1, 15);
        User u2 = new User(2, 10);
        User u3 = new User(3, 18);
        User u4 = new User(4, 8);
        User u5 = new User(5, 11);
        list.add(u1);
        list.add(u2);
        list.add(u3);
        list.add(u4);
        list.add(u5);
        SelectSort.selectSort(list);
    }

    /**
     * 选择排序  外层一次 里层一圈 每次只找最小交换 外层交换
     * @param list
     */
    private static void selectSort(List<User> list) {
        for (int i = 0; i < list.size(); i++) {

            User userI = list.get(i);

            int minIndex = i;
            int minAge= userI.getAge();
            for (int j = i + 1; j < list.size(); j++) {
                User userJ = list.get(j);

                if (minAge > userJ.getAge()) {
                    minAge = userJ.getAge();
                    minIndex = j;

                }

            }
            if(minIndex==i){
                continue;
            }
//            交换
            int  tempAge = list.get(minIndex).getAge();
            int tempId = list.get(minIndex).getId();
            User temp = new User(tempId, tempAge);
            list.set(i, temp);
            list.set(minIndex, userI);

        }
        System.out.println(JSONObject.toJSONString(list));
    }
}
